<?php
/**
 * Created by PhpStorm.
 * User: sunlight
 * Date: 2018/11/19
 * Time: 16:21
 */

namespace app\common\model;


class Product extends BaseModel{

    /**
     * 后台自动化分页
     * @param array $data
     * @return array
     */
    public function getProduct($data=[]){
        $data['status']=[
            'neq', config('code.status_delete')
        ];

        $order = ['id' => 'desc'];

        // 查询
        $result=$this->where($data)->order($order)->paginate();

        //调试
        //echo $this->getLastSql();

        return $result;
    }

    /**
     * 根据条件来获取列表的数据
     * @param array $condition
     * @param int $from
     * @param int $size
     */
    public function getProductByCondition($condition = [], $from=0, $size = 5){
       if (!isset($condition['status'])){
            $condition['status']=[
                'neq', config('code.status_delete')
            ];
        }

        $order = ['id' => 'desc'];

        $result=$this->where($condition)
            ->field($this->_getListField())
            ->limit($from,$size)
            ->order($order)->select();

        //调试
        //echo $this->getLastSql();

        return $result;
    }

    /**
     * 根据条件来获取列表的数据的总数
     * @param array $condition
     */
    public function getProductCountByCondition($condition=[]){
        if (!isset($condition['status'])){
            $condition['status']=[
                'neq', config('code.status_delete')
            ];
        }

        return $this->where($condition)->count();

        //echo $this->getLastSql();
    }

    /**
     * 获取首页头图数据
     * @param int $num
     * @return array
     */
    public function getIndexHeadNormalProduct($num = 5){
        $data=[
            'status' => 1,
            'is_position' => 1
        ];

        $order = [
            'id' => 'desc',
        ];
        return $this->where($data)
            ->field($this->_getListField())
            ->order($order)->limit($num)->select();
    }

    /**
     * 获取推荐的数据--技师
     * @param int $num
     * @return array
     */
    public function getPositionNormalProduct($num = 20){
        $data=[
            'status' => 1,
            'is_position' => 1,
        ];

        $order = [
            'id' => 'desc',
        ];

        return $this->where($data)
            ->field($this->_getListField())
            ->order($order)->limit($num)->select();
    }

    /**
     * 获取排行榜数据
     * @param int $num
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRankNormalNews($num=5){
        $data = [
            'status' => 1,
        ];

        $order = [
            'read_count' => 'desc',
        ];

        return $this->where($data)
            ->field($this->_getListField())
            ->order($order)->limit($num)->select();
    }

    /**
     * 通用化获取参数的数据字段
     * @return array
     */
    private function  _getListField(){
        return [
            'id',
            'title',
            'small_title',
            'age',
            'height',
            'weight',
            'catid',
            'address',
            'img',
            'num',
            'status',
            'is_approve',
            'is_position',
            'read_count',
        ];
    }

}